window.workspace = function(){
	var response  = <?#$respons?>;
	var store, grid, win, form;
	var sid = {'status':Ext.id()};
	function addForm(rows){
		if(!win) {
		    form = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 120,
		        fileUpload  : true,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	: 'textfield'
		    	},
		    	items		: [{
					xtype		: 'hidden',
					name		: 'id',
					value		: 0
				},{
					xtype		: 'textfield',
					fieldLabel	: '贴图描述[Notes]',
					width		: 240,
					name		: 'notes',
					maxLength   : 255,
					allowBlank	: false
				},{
					xtype		: 'numberfield',
					fieldLabel	: '排序-小到大[Rank]',
					width		: 120,
					value       : 0,
					name		: 'rank',
					allowBlank	: false
				},{
		    		xtype		: 'combo',
		            store		: new Ext.data.ArrayStore({
			        	fields: ['id','name'],
			        	data: main.cv2Array(response.use)
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					mode			: 'local',
		            fieldLabel		: '使用环境[Usage]',
		            width			: 120,
		            allowBlank		: false,
					triggerAction	: 'all',
					hiddenName		: 'use',
		            name			: 'use'
		        },{
		    		xtype		: 'combo',
		            store		: new Ext.data.ArrayStore({
			        	fields: ['id','name'],
			        	data: main.cv2Array(response.status)
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					mode			: 'local',
		            fieldLabel		: '状态[Status]',
		            width			: 120,
		            allowBlank		: false,
					triggerAction	: 'all',
					hiddenName		: 'status',
		            name			: 'status'
		        },{
					xtype		: 'textfield',
					inputType 	: 'file', 
			 		fieldLabel	: '预览图[Preview]',
					name		: 'Fileicon'
				},{
					xtype		: 'textfield',
					inputType 	: 'file', 
			 		fieldLabel	: '资源地址[Src]',
					name		: 'Filesrc'
				}]
			});
			win = new Ext.Window({
				title	: '新增[Add]',
				iconCls	: 'icon-add',	
				width	: 420,
				items	: [form],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text	: '保存[Save]', 
						iconCls	: 'icon-save',
						url    	: '/admin/live/config/chartlet/dosave.html',
						handler:function(){
						if(!form.getForm().isValid())
		        			return;
		        		form.getForm().submit({
		        			waitMsg		: GLang['30017'],
							waitTitle 	: GLang['30003'],
			        		clientValidation: true,
			        		url     : '/admin/live/config/chartlet/dosave.html',
				        	success	: function(form, action) {
				        		store.reload();
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		var id = Ext.isObject(rows)? rows.id:0;
		win.setTitle((id > 0)? '新增[Edit]':'新增[Add]');
		win.setIconClass((id > 0)? 'icon-edit':'icon-add');
		win.show();
		form.getForm().findField('Fileicon').setRawValue('');
		form.getForm().findField('Filesrc').setRawValue('');
		(id < 1)? form.getForm().reset():form.getForm().loadRecord(rows);
	};
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url			: '/admin/live/config/chartlet/dolist.html',
	    remoteSort	: true,
	    sortInfo	: {
	            field		: 'id',
	            direction	: 'DESC'
		},
		baseParams	: {
			start	: 0,
			limit	: main.pageSize
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'notes', 'use', 'rank', 'src', 'icon', 'status']
	});
	
 	grid = {
		xtype		: 'grid',
		title		: response.title,
	    store		: store,
		region		: 'center', 
	    columns		: [
	        {header: 'ID编号', width:60, sortable:true, dataIndex:'id'},
			{header: '贴图描述[Notes]', width:160, dataIndex:'notes'},
			{header: '预览图[Preview]', width:100, dataIndex:'icon', renderer:function(v){
				if(/\.(png|jpg|jpeg|gif|bmp)$/i.test(v)) {
					 return '<img src="'+Ext.STATIC_URL+v+'" height="25" />';
				}
				return '';
			}},
			{header: '资源地址[Src]', width:240, dataIndex:'src'},
			{header: '排序-从小到大[Rank]', width:80, sortable:true, dataIndex:'rank'},
			{header: '状态[Status]', width:80, sortable:true, dataIndex:'status', renderer:function(v){
				return response.status[v];
			}},
			{header: '使用环境[Usage]', width:80, sortable:true, dataIndex:'use', renderer:function(v){
				return response.use[v];
			}}
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
	    listeners	: {
	    	'destroy':function(){
    			win && win.close();
    		},'rowdblclick':function(grid, index, e){
		    	var data = grid.store.getAt(index).copy();
		    	addForm(data);
		    }, 'render':function(obj){
				main.gridRender(obj, ['icon']);
	    	}
    	},
	    sm			: new Ext.grid.RowSelectionModel({singleSelect:true}),
	    stripeRows	: true,
	    tbar		: [{
			text	: '刷新[Reload]',
			iconCls	: 'icon-refresh',
			handler : function(){
				store.reload(); 
			}
	    },'-',{
			text	: '新增[Add]',
			iconCls	: 'icon-add',
			url     : '/admin/live/config/chartlet/dosave.html',
			handler : function() {
				addForm(0);
			}
		},'-',{
			text	: '删除[Del]',
			iconCls	: 'icon-delete',
			url     : '/admin/live/config/chartlet/dodelete.html',
			handler : function(){
				var rcs = grid.sm.getSelections();
				if(Ext.isEmpty(rcs)) {
					Ext.Msg.alert(GLang['30003'], GLang['30020']);
					return;
				}
				var ids= [];
				Ext.each(rcs, function(item){
					ids.push(item.get('id'));
				});
				Ext.Msg.confirm(GLang['30003'], GLang['30025'], function(btn){
					if(btn=="yes") {
						Ext.Ajax.request({
							url		: '/admin/live/config/chartlet/dodelete.html',
							params	: {'ids':ids.join(',')},
							success : function(resp, req) {
								var data = Ext.decode(resp.responseText);
								if(data.code == 0) {
									store.reload(); 
								} else {
									Ext.Msg.alert(GLang['30003'], data.msg);
								}
							}
						});
					}
				});
			}
		},'-','状态:',{
			xtype		: 'combo',
			store		: new Ext.data.ArrayStore({
		        fields: ['id','name'],
		        data: main.cv2Array(response.status)
		    }),
			displayField	: 'name',
			valueField		: 'id',
			mode			: 'local',
			triggerAction	: 'all',
			width			: 80,
			id  			: sid.status
		},'-',{
			text			: '检索',
			iconCls			: 'icon-search',
			handler: function() {
				store.baseParams.status   = Ext.getCmp(sid.status).getValue();
				store.reload(); 
			}
		}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};
	return grid;
}();